package es.udc.emarketapp.emarket.model.shippingFacade.actions;

import java.sql.Connection;
import java.util.List;

import es.udc.emarketapp.emarket.model.shipping.dao.SQLShippingDAO;
import es.udc.emarketapp.emarket.model.shipping.dao.SQLShippingDAOFactory;
import es.udc.emarketapp.emarket.model.shipping.vo.ShippingVO;
import es.udc.emarketapp.emarket.model.shippingFacade.vo.ShippingsCountryVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;

import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.NonTransactionalPlainAction;


public class ModifyShippingsCountryAction implements NonTransactionalPlainAction {

    private ShippingsCountryVO shippingsCountry;

    public ModifyShippingsCountryAction(ShippingsCountryVO shippingsCountry) {
        this.shippingsCountry = shippingsCountry;
    }

    public Object execute(Connection connection)
            throws InternalErrorException {


        SQLShippingDAO shippingDAO = SQLShippingDAOFactory.getDAO();

        String idSeller = shippingsCountry.getIdSeller();
        String idCountry = shippingsCountry.getIdCountry();
        List<Double> weightRanges = (List) shippingsCountry.getWeightRanges();
        List<Double> priceRanges = (List)shippingsCountry.getPriceRanges();

        
       int i = 0;
       ShippingVO shipping;
        while (i < weightRanges.size()) {

             shipping = new ShippingVO(idSeller, idCountry,
                   weightRanges.get(i), priceRanges.get(i));
            

            try {
                shippingDAO.update(connection, shipping);
            } catch (InstanceNotFoundException e) {
                throw new InternalErrorException(e);
            }

             i++;

        }

        return shippingsCountry;

    }
}
